<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="register.html">注册</a>
        <a href="activationAccount.html" target="_blank">用户激活</a>
    </div>
    <!-- 版心 -->

    <!-- 页面主体部分 -->
    <div class="login-container">
        <!-- 垂直水平居中的登录对话框 -->
        <div class="login-dialog">
            <!-- 登录对话框的头部信息 -->
            <div class="header">
                <span>用户登录</span>
                <div class="space"></div>
                <span>没有账号,去<a href="register.html">注册</a></span>
            </div>
            <!-- 登录对话框的登录部分 -->
            <div class="login-part">
                <h3>欢迎登录</h3>
                <div class="row">
                    <span>账号：</span>
                    <input type="text" id="username" name="username" maxlength="16">
                </div>
                <div class="row">
                    <span>密码：</span>
                    <input type="password" id="password" name="password" maxlength="24">
                </div>
                <div class="row">
                    <span>验证码</span>
                    <input type="text" id="verificationCodeInput"><br>
                    <img id="verificationCodeImage" src="#" alt="图片验证码"/>
                </div>
                <div class="row">
                    <button id="submit" onclick="mysub()">登录</button>
                </div>
            </div>
        </div>
    </div>

</body>

<script>
    // 初始化图片验证码标签点击事件
    function initVerificationCodeImage(){
        // 当页面完毕后获取图片验证码
        getVerificationCodeImage();
        // 绑定图片点击事件
        let verificationCodeImage = document.querySelector('#verificationCodeImage');
        verificationCodeImage.onclick = function(){
            // 获取新的图片验证码
            getVerificationCodeImage();
        }
    }
    initVerificationCodeImage()

    function getVerificationCodeImage(){
        // 获取图片验证码
        jQuery.ajax({
            url: "/user/getImageVerificationCode",
            type: 'GET',
            success: function(body){
                if(body.code === 200){
                    //获取验证码图片标签
                    let img = document.querySelector('#verificationCodeImage');
                    // 修改图片标签的路径
                    img.src = body.data;
                    // jQuery("#verificationCodeImage").attr('src', body.data);
                }
            }
        });
    }


    function mysub() {
        let username = jQuery('#username');
        let password = jQuery('#password')
        let verificationCode = jQuery('#verificationCodeInput');

        if(username.val().trim() == ""){
            alert("请输入用户名！");
            username.focus();
            return false;
        }

        if(password.val().trim() == ""){
            alert("请输入用户密码！");
            password.focus();
            return false;
        }

        if(verificationCode.val().trim() == ""){
            alert("请输入图形验证码！");
            verificationCode.focus();
            return false;
        }

        jQuery.ajax({
            url: "/user/login",
            type: "post",
            data: {
                "username": username.val(),
                "password": password.val(),
                "code": verificationCode.val()
            },

            success: function (res) {
                console.log(res);
                if (res.code == 200 && res.data == 1) {
                    alert("用户登录成功！");
                    location.href = "myblog_list.html";
                } else if (res.code == -3) {
                    // 账号未激活状态，
                    if (confirm("账号未激活！是否到激活页面进行激活操作？")) {
                        // 进行激活操作，跳转至账号激活页面
                        window.open("activationAccount.html");

                    } else {
                        // 不进行激活操作，不跳转至账号激活页面
                        location.href = location;

                    }
                } else {
                    alert("用户登录失败！" + res.message);
                    location.href = "login.html";
                }
            }
        });
    }
</script>
</html>